/*
 * Author xtt
 * 2018.03.02
 */
(function () {
    "use strict";
    var
        self = this,
        D;
    D = {
        __BUILD_TIEM: '2018-3-7',
        Version: '0.0.1',
        _mix: function (target, resource) {
            for (var name in resource) {
                if (resource.hasOwnProperty(name)) {
                    target[name] = resource[name];
                }
            }
        }
    };
    //高级搜索
    $('.highOpt').click(function () {
        var o = $(this).hasClass('up');
        if (o) {
            $(this).next('.high-search').hide();
            $(this).removeClass('up');
            $(this).text('高级查询');
        } else {
            $(this).next('.high-search').show();
            $(this).addClass('up');
            $(this).text('收起查询');
        }
    })
    //开关
    $('.icass-switch').each(function () {
        var that = $(this);
        // that.hide();
        var c = that.prop("checked");
        var sw = '<span class="icass-switch-warp"></span>';
        c ? sw = '<span class="icass-switch-warp switch-on"></span>' : '';
        that.after(sw);

    });
    $(document).on('click', '.icass-switch-warp', function () {
        var isHas = $(this).hasClass('switch-on');
        console.log(isHas)
        // // debugger
        if (isHas) {
            $(this).removeClass('switch-on');
            $(this).prev('input').prop("checked", false);
        } else {
            $(this).addClass('switch-on');
            $(this).prev('input').prop("checked", true);
        }
        $('.icass-switch').change()
    });
    'DGG' in self || ( self['DGG'] = D);
}).call(this);

(function (W, $, D) {
    "use strict";
    var
        $body = $('body'),
        // iframe DOM
        $iframeWarp = $('#iframe-warp'),
        _getIframeHmtl;
    /**
     * Git Iframe_Hmtl
     */
    _getIframeHmtl = function (url) {
        var hei = ($(W).height() - $iframeWarp.offset().top - 10) - 29;
        return ' <iframe scrolling="auto" frameborder="0"  src="' + url + '" style="width:100%;height:' + hei + 'px;"></iframe>';
    };
    D._mix(D, {
        /**
         * iframe
         * @param title
         * @param url
         * @param closable
         * @returns {boolean}
         */
        addTab: function (title, url, closable) {
            if ($iframeWarp.tabs("exists", title)) {
                $iframeWarp.tabs("select", title);
                return !1;
            }
            url += (url.indexOf("?") > -1 ? "&" : "?") + "is_first_request=1";
            $iframeWarp.tabs('add', {
                title: title,
                content: _getIframeHmtl(url),
                closable: closable || !0
            });
        },
        /*
		 * 更新tab,若存在刷新，不存在添加
		 * @param title tab标题
		 * @param url 路径
		 */
        updateTab: function (title, url) {
            $iframeWarp.tabs('select', title);
            var tab = $iframeWarp.tabs('getSelected');
            if ($iframeWarp.tabs('exists', title)) {
                var content = _getIframeHmtl(url);
                $iframeWarp.tabs('update', {
                        tab: tab,
                        options: {
                            title: title,
                            content: content
                        }
                    }
                );
            } else {
                DGG.addTab(title, url);
            }
        },
        /*
         * 刷新指定tabs
         * @param title tab标题
         * @param url 路径
         */
        tabsUpdate: function (title, url) {
            var content = _getIframeHmtl(url);
            var tab = $iframeWarp.tabs('getTab', title);
            var index = $iframeWarp.tabs('getTabIndex', tab);
            if ($iframeWarp.tabs('exists', title)) {
                $iframeWarp.tabs('update', {
                        tab: tab,
                        options: {
                            title: title,
                            content: content
                        }
                    }
                );
            } else {
                DGG.addTab(title, url);
            }

        },
        /*
         * 关闭指定tabs
         * @param tabName tab标题
         */
        assignClose: function (tabName) {
            $iframeWarp.tabs('close', tabName);
        },
        /*
         * 更新选项卡标题
         * @param oldTitle tab标题
         * @param newTitle tab更改的标题
         */
        updateTabTitle: function (oldTitle, newTitle) {
            $iframeWarp.find('li').find('.tabs-title').filter(function () {
                return $(this).html() == oldTitle;
            }).html(newTitle);
        },
        /*
        * tab上刷新操作
        * */
        refreshTab: function () {
            var url = "";
            $(".panel").each(function () {
                var thisobj = $(this);
                if ($(this).css("display") == "block") {
                    url = thisobj.children("div").children("iframe").attr("src");
                }
            });
            var current_tab = $iframeWarp.tabs('getSelected');
            $iframeWarp.tabs('update', {
                tab: current_tab,
                options: {
                    content: _getIframeHmtl(url)
                }
            });
        },

    });
    $body
        .on('click', '.dgg-tool-menu [data-url]', function () {
            var $this = $(this), menuData = $this.attr('data-url');
            try {
                // 将字符串解析成json对象(new Function("","return "+json))();
                menuData = new Function('return ' + menuData)();
            } catch (e) {
                console.error(' menuData element property data-url error' + menuData);
            }
            var options = $.extend({
                elem: this,
                data: []
            }, menuData);
            D.addTab(options.name, options.url);
        });


})(window, jQuery, DGG);


(function (W, $, D) {
    var isStop;
    _closeTime = function (t, i) {
        isStop = setInterval(function () {
            layer.close(i)
        }, t);
    };
    mouseFun = function (t, i) {
        var close = true;
        $('.layui-layer-dialog').mouseenter(function () {
            close = false;
            clearInterval(isStop);
        });
        $('.layui-layer-dialog').mouseleave(function () {
            close = true;
            _closeTime(t, i)
        });
        close && _closeTime(t, i);
    };
    _alert = function (m, t, options) {
        var e = (typeof (t) == "object" || t == undefined);
        var o = {
            title: false,
            skin: 'layer-alerts'
        };
        e && (options = t, t = '');
        o = $.extend({}, o, options);
        var index = layer.alert('<b>' + t + '</b><p>' + m + '</p>', o)
        return index;
    }
    D._mix(D, {
        /**
         * 操作成功消息提示
         * @param m 提示信息文字内容
         * @param options layer自带参数以对象的形式传入
         * @param c 点击“确认”后操作回调
         * @param d 点击“取消”后操作回调
         * @param e 关闭加载层，需传的参“close”
         */
        loading: function (close, options) {
            var e = (typeof (close) == "object");
            e && (options = close);
            if (close == 'close') {
                layer.closeAll('loading');
                return false;
            }
            var o = {
                time: 0
            };
            o = $.extend({}, o, options);
            layer.load(0, o);
        },
        Info: function (m, t, options) {
            t = 1500;
            var o = {
                time: 0,
                closeBtn: 1,
                icon: 0,
                skin: 'layer-info',
            };
            var o = $.extend({}, o, options);
            var index = layer.msg(m, o);
            mouseFun(t, index)
        },
        Succeed: function (m, t, options) {
            t = 1500;
            var o = {
                time: 0,
                closeBtn: 1,
                icon: 1,
                skin: 'layer-succeed',
            };
            o = $.extend({}, o, options);
            var index = layer.msg(m, o);
            mouseFun(t, index)
        },
        Warning: function (m, t, options) {
            t = 1500;
            var o = {
                time: 0,
                closeBtn: 1,
                icon: 0,
                skin: 'layer-warning',
            };
            o = $.extend({}, o, options);
            var index = layer.msg(m, o);
            mouseFun(t, index)
        },
        Errors: function (m, t, options) {
            t = 1500;
            var o = {
                time: 0,
                closeBtn: 1,
                icon: 2,
                skin: 'layer-error',
            };
            o = $.extend({}, o, options);
            var index = layer.msg(m, o);
            mouseFun(t, index)
        },
        Alert_succeed: function (m, t, options) {
            _alert(m, t, {icon: 0})
        },
        Alert_error: function (m, t, options) {
            _alert(m, t, {icon: 1})
        },
        Alert_warning: function (m, t, options) {
            _alert(m, t, {icon: 2})
        },
        Alert_info: function (m, t, options) {
            _alert(m, t, {icon: 3})
        },
        Confire: function (m, t, c, options, d) {
            var e = $.isFunction(t),
                o = {
                    title: false,
                    btn: ["&#x786E;&#x5B9A;", "&#x53D6;&#x6D88;"],
                    skin: 'layer-confire'
                };
            e && (d = options, options = c, c = t, t = '')
            o = $.extend({}, o, options);
            var msg = '<b>' + t + '</b><p>' + m + '</p>';
            var index = layer.confirm(msg, o, function (i) {
                c && $.isFunction(c) && c.call(this), layer.close(i)
            }, function () {
                d && $.isFunction(d) && d.call(this)
            });
        },
        Del: function (m, c, options, d) {
            var e = $.isFunction(m),
                o = {
                    title: false,
                    btn: ["删除", "&#x53D6;&#x6D88;"],
                    skin: 'layer-del'
                };
            e && (d = options, options = c, c = m, m = '删除该数据后不可恢复，是否删除？')
            o = $.extend({}, o, options);
            var index = layer.confirm('<b>删除确认</b><p>' + m + '</p>', o, function (i) {
                c && $.isFunction(c) && c.call(this), layer.close(i)
            }, function () {
                d && $.isFunction(d) && d.call(this)
            });
        },
        Compel: function (m, options) {
            var o = {
                title: false,
                btn: ["知道了"],
                skin: 'layer-compel'
            };
            o = $.extend({}, o, options);
            var index = layer.alert('<b>温馨提示</b><p>' + m + '</p>', o)
            return index;
        },
        Tip: function (m, e, options) {
            var o = {
                tips: [1, '#ff6767'],
                time: 0,
                skin: 'layer-tip'
            };
            o = $.extend({}, o, options);
            layer.tips(m, e, o);
        },
        Open: function (options) {
            var o = {
                title: false,
                btn: ["知道了"],
                skin: 'layer-compel'
            };
            var o = {
                btn: ["&#x786E;&#x5B9A;", "&#x53D6;&#x6D88;"],
                skin: 'layer-open',
            };
            o = $.extend({}, o, options);
            layer.open(o);
        }
    })

})(window, jQuery, DGG);
/*
 
 * */
(function (W, $, D) {
    D._mix(D, {
        /**
         * 前端搜索
         * @param options 带两个参数，inputEle输入框元素的id或class，exportEle展示筛选结果列表id或class
         */
        search: function (options) {
            var o = {
                inputEle: '',
                exportEle: ''
            }
            o = $.extend({}, o, options);
            $(o.inputEle).keyup(function () {
                var i = $.trim($(o.inputEle).val());
                i == '' && $(o.exportEle).children().show();
                $(o.exportEle + ' li:contains(' + i + ')').show();
                $(o.exportEle + ' li:not(:contains(' + i + '))').hide();
            })
        }
    })
})(window, jQuery, DGG);

(function (W, $, D) {
    _errorStyle = function (e, m) {
        $(e).addClass('error-tips');
        var o = {
            tips: [1, '#e78081']
        }
        // o=$.extend({},o,options);
        layer.tips(m, $(e), o);
    }
    _isNull = function (e, l) {
        var t = "该字段不允许为空";
        var v = $(e).val();
        console.log(l)
        // (v!=null&&v!='');
        if (v == null || v == '') {
            _errorStyle(e, t);
            return false;
        }
        console.log(l)
        // debugger
        if (l != '' && l != null) {
            (typeof (l) == "number") && (v.length <= l) && _errorStyle(e, '该字段不允许为空');
            console.log(typeof (l))
        }
    };
    D._mix(D, {
        verify: function (type, e, options) {
            var o = {
                length: ''
            }
            o = $.extend({}, o, options);
            console.log(o.length)
            switch (type) {
                case 'isNull':
                    _isNull(e, o.length)
            }
        }
    })
})(window, jQuery, DGG);

(function (W, $, D) {
    //收起下拉
    _closeSelect = function (ele, op) {
        var isOpen = $(ele).hasClass('open');
        var cla = ele.substr(1, ele.length);
        var v, t;
        if (isOpen) {
            t = $(ele + '.open').val();
            v = $(ele + '.open').find('option:selected').text();
            $(ele).next().children('input').val(v).attr("data-value", t);
            $(op).remove();
            $(ele).removeClass('open');
            $('.' + cla + '-select').removeClass('open');
        }
    };
    D._mix(D, {
        /*
        * 下拉搜索，前端模糊搜索
        * */
        selectSearch: function (e, options, c) {
            var isFun = $.isFunction(c);
            var ele = $(e), cla = e.substr(1, e.length);
            ele.hide();
            var skin = ele.attr("class");
            var o = {
                width: "200px"
            }
            o = $.extend({}, o, options);
            var wi = o.width;
            var w = '.dgg-select-warp',//输入框外层
                ex = '.' + cla + '-select',
                op = '.' + cla + '-open',//下拉展开外层
                c = '.dgg-select-opt',//操作按钮外层
                clearAll = '.clear-val',//清除单个值
                d = $(document),
                b = $('body');
            var label = '<label class="dgg-select-warp ' + cla + '-select">' +
                '<input type="text" class="icass-input">' +
                '<a href="javascript:void(0)" class="clear-val"><i class="icass-icon remove"></i></a>' +
                '<span class="triangle"></span>' +
                '</label>';
            var htm = '<div class="dgg-select-out ' + skin + ' ' + cla + '-open"><ul>';
            ele.after(label);
            var pl = parseInt($('.' + cla + '-select').children('input').css('padding-left')),
                pr = parseInt($('.' + cla + '-select').children('input').css('padding-right')),
                bw = parseInt($('.' + cla + '-select').children('input').css('border-width')) * 2;
            var setW = parseInt(wi) - (pl + pr + bw);
            $('.' + cla + '-select').children('input').css("width", setW);
            ele.children('option').each(function () {
                var t = $(this).text(),
                    v = $(this).val(),
                    selected = $(this).prop("selected"),
                    that = $(this);
                if (selected) {
                    that.parent().next().children('input').val(t).attr("data-value", v);
                }
                htm += '<li  data-value="' + v + '">' + t + '</li>';
            });
            htm += '</ul><p>未找到匹配结果</p>';
            htm += '<div class="dgg-select-opt">';
            if (o.btn != '' && o.btn != undefined) {
                for (var i = 0; i < o.btn.length; i++) {
                    htm += '<a href="javascript:void(0)" data-sort="' + i + '">' + o.btn[i];
                    // console.log(options.icon)
                    if (o.icon != '' && o.icon != 'undefined') {
                        (o.icon[i] != undefined && o.icon[i] != '') && (htm += '<i class="icass-icon ' + o.icon[i] + '"></i>');
                    }
                    htm += '</a>'
                }
            }
            htm += '</div></div>';
            //点击展开下拉
            $(ex).children('input').click(function () {
                var _this = $(this).parent()
                var t = _this.offset().top + $(this).outerHeight() - 1,
                    l = _this.offset().left;
                var isOpen = _this.hasClass('open');
                if (!isOpen) {
                    _this.addClass('open').prev('select').addClass('open');
                    b.append(htm);
                    $(op).css({
                        "top": t,
                        "left": l,
                        "width": parseInt(wi) - 2
                    })
                }
                $(this).keyup();
            });
            //选中选项
            d.on('click', '.' + cla + '-open li', function () {
                var t = $(this).attr('data-value'),
                    v = $(this).text();
                $(ex).prev().find("option[value='" + t + "']").prop("selected", true);
                isFun && c.call();
                _closeSelect(e, op)
            });
            //搜索
            ele.next().find('input').keyup(function () {
                var v = $.trim($(this).val());
                v == '' && $(op).find('li').show();
                var resLen = $(op + ' li:contains(' + v + ')').length;
                $(op + ' li:contains(' + v + ')').show().addClass('include');
                $(op + ' li:not(:contains(' + v + '))').hide().removeClass('include');
                $(op + ' li.include').removeClass('active');
                $(op + ' li.include').eq(0).addClass('active');
                if (resLen == 0) {
                    $(op).children('ul').hide();
                    $(op).children('p').show();
                } else {
                    $(op).children('ul').show();
                    $(op).children('p').hide();
                }
            });
            //操作按钮
            d.on('click', op + ' ' + c + ' a', function () {
                var num = $(this).attr("data-sort");
                if (num == 0) {
                    num = '';
                }
                if (o['btnFun' + num] != undefined) {
                    o['btnFun' + num]();
                }
            });
            //清除选中值
            d.on('click', e + '+.dgg-select-warp ' + clearAll, function () {
                var t = $(this).parent().children('input').attr('data-value');
                $(this).parent().children('input').val('');
                $(this).parent().prev('select').find("option[value='" + t + "']").prop("selected", false);
                console.log($(this).parent().prev('select').val())
            })
            //点击桌面关闭
            d.on('click', function (event) {
                // console.log($('.dgg-select-out').length)
                var case1 = $(event.target).parents('.dgg-select-warp').length,
                    case2 = $(event.target).parents('.dgg-select-out').length;
                if (case1 == 0 && case2 == 0) {
                    _closeSelect(e, op)
                }
            })
        },
        clearSelectSearch: function (e) {
            var t = $(e).next().find('input').attr('data-value')
            $(e).next().find('input').val('').attr('data-value', '');
            $(e).find("option[value='" + t + "']").prop("selected", false);
        }
    })
})(window, jQuery, DGG);

(function (W, $, D) {
    _init = function () {
        var label = '<label class="dgg-select-warp ' + cla + '-select"><input type="text" class="icass-input"><span class="triangle"></span></label>';
    }
    D._mix(D, {
        select: function (e, options) {
            
        }
    })
})(window, jQuery, DGG);
